package com.hg.oa.module.daka.presenter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.Poi;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.hg.oa.R;
import com.hg.oa.common.BasePresenterImpl;
import com.hg.oa.module.daka.model.LocationRec;
import com.hg.oa.util.DateUtil;
import com.hg.oa.util.LocationUtils;
import com.hg.oa.util.PermissionsUtil;
import com.hg.oa.util.ViewUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by fengxiang on 2016/9/21.
 */
public class DakaPresenter extends BasePresenterImpl {

    String tag = "DakaPresenter";

    public DakaPresenter(Context context) {
        super(context);
    }

    public void checkAndRequestLocPermission(Activity activity) {
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // 申请一个（或多个）权限，并提供用于回调返回的获取码（用户定义)
            ActivityCompat.requestPermissions(activity, new String[]{
                            Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION
                    },
                    PermissionsUtil.ACCESS_LOCATION_CODE);
        } else {
            LocationUtils.start();
        }
    }

    /**
     * 设置打卡界面数据
     *
     * @param isMorning
     * @param locationRec 定位数据
     * @param timeView    上午打卡时间
     * @param locView     上午打卡位置
     * @param timeView2   下午打卡时间
     * @param locView2    下午打卡位置
     */
    public void setValue(boolean isMorning, LocationRec locationRec, TextView timeView, TextView locView, TextView timeView2, TextView locView2) {
        if (isMorning) {
            setMorningValue(locationRec, timeView, locView);
        } else {
            setAfternoonValue(locationRec, timeView2, locView2);
        }
    }

    protected void setMorningValue(LocationRec locationRec, TextView timeView, TextView locView) {
        timeView.setText(DateUtil.DateToString(new Date(), DateUtil.DateStyle.HH_MM));
        locView.setText(locationRec.location.getStreet() + locationRec.location.getStreetNumber() + " " + locationRec.poi.getName());
    }

    protected void setAfternoonValue(LocationRec locationRec, TextView timeView, TextView locView) {
        timeView.setText(DateUtil.DateToString(new Date(), DateUtil.DateStyle.HH_MM));
        locView.setText(locationRec.location.getStreet() + locationRec.location.getStreetNumber() + " " + locationRec.poi.getName());
    }

    public LocationRec onReceiveLocation(BDLocationListener listener, BDLocation location, GeoCoder mSearch) {
        LocationUtils.stop();
        LocationUtils.unRegister(listener);
        LatLng ptCenter = new LatLng(location.getLatitude(), location.getLongitude());
        // 反Geo搜索
        mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ptCenter));
        List<Poi> list = location.getPoiList();// POI数据

        Poi poi1 = list.get(0);
        for (Poi poi : list) {
            Log.e(tag, poi.getName());
            if (poi.getName().equals("解放日报大厦")) {
                poi1 = poi;
                break;
            }
        }
        return new LocationRec(location, poi1);
    }

    public void onGetReverseGeoCodeResult(BaiduMap baiduMap, ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(context, "抱歉，未能找到结果", Toast.LENGTH_LONG)
                    .show();
            return;
        }
        baiduMap.clear();
        baiduMap.setMaxAndMinZoomLevel(20, 20);
        baiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
                .icon(BitmapDescriptorFactory
                        .fromResource(R.mipmap.icon_gcoding)));
        baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result.getLocation()));
    }

    public boolean isMorningOrAfternoon(View morningLayout, View afternoonLayout) {
        Date date = new Date();
        int hours = date.getHours();
        if (hours >= 0 && hours <= 12) {
            ViewUtil.show(morningLayout);
            ViewUtil.hide(afternoonLayout);
            return true;
        } else if (hours > 12 && hours <= 23) {
            ViewUtil.hide(morningLayout);
            ViewUtil.show(afternoonLayout);
            return false;
        }
        return true;
    }
}
